package com.woniuxy.yoga.commons.web.exception;

import com.woniuxy.yoga.commons.core.exception.YogaException;
import com.woniuxy.yoga.commons.core.model.Result;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.security.SignatureException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理系统异常
     */
    @ExceptionHandler(Exception.class)
    public Result<String> systemException(Exception e){
        e.printStackTrace();
        //记录日志
        return Result.getResult(String.class).setCode(500).setMessage(StringUtils.hasLength(e.getMessage())?e.getMessage():"系统异常");
    }

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(YogaException.class)
    public Result<String> yogaException(YogaException e){
        e.printStackTrace();
        //记录日志
        return Result.getResult(String.class).setMessage(e.getMessage()).setCode(e.getCode());
    }

    /**
     * 处理参数校验
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> methodArgumentNotValidException(MethodArgumentNotValidException e){
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        allErrors.forEach(o-> System.out.println(Objects.requireNonNull(o.getCodes())[0]+"=="+o.getDefaultMessage()));
        return Result.getResult(String.class).setCode(500).setMessage(Objects.requireNonNull(allErrors.get(0).getCodes())[0]+":"+allErrors.get(0).getDefaultMessage());
    }

    /**
     * 处理参数校验
     */
    @ExceptionHandler(BindException.class)
    public Result<String> bindException(BindException e){
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        allErrors.forEach(o-> System.out.println(Arrays.toString(o.getCodes()) +"=="+o.getDefaultMessage()));
        return Result.getResult(String.class).setCode(500).setMessage(Objects.requireNonNull(allErrors.get(0).getCodes())[0]+":"+allErrors.get(0).getDefaultMessage());
    }

    /**
     * jwt格式错误
     */
    @ExceptionHandler(MalformedJwtException.class)
    public Result<String> malformedJwtException(MalformedJwtException e){
        e.printStackTrace();
        return Result.getResult(String.class).setCode(401).setMessage("Jwt格式错误");
    }
    /**
     * jwt签名错误
     */
    @ExceptionHandler(SignatureException.class)
    public Result<String> signatureException(SignatureException e){
        e.printStackTrace();
        return Result.getResult(String.class).setCode(401).setMessage("Jwt签名错误");
    }
    /**
     * jwt已过期
     */
    @ExceptionHandler(ExpiredJwtException.class)
    public Result<String> jwtException(ExpiredJwtException e){
        e.printStackTrace();
        return Result.getResult(String.class).setCode(401).setMessage("Jwt已过期");
    }
}
